package onlinebanking.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import onlinebanking.server.SimpleServer;

//ilang instance ba ng ganito ang meron sa buong runtime???

/**
 * 
 * @author Del Mundo A class used to connect directly to the database driver
 *         Singleton design - Only one instance can exist at runtime.
 */
public class SQLConnection {

	/**
	 * The only instance of the SQLConnection.
	 */
	private volatile static SQLConnection connectionInstance = null;

	/**
	 * Connection to the database.
	 */
	private Connection connection = null;

	/**
	 * Name of the database driver
	 */
	protected final String driverName = "com.mysql.jdbc.Driver";

	/**
	 * Address of Server
	 */
	private String serverName = "localhost";

	/**
	 * Port Number
	 */
	private int serverPort = 3306;

	/**
	 * Name of Database
	 */
	private String mydatabase = "OnlineBanking";

	/**
	 * Name of User
	 */
	private String username = "root";

	/**
	 * Password
	 */
	private String password = "";

	/**
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws SQLException
	 * 
	 */
	private SQLConnection() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {
		// if (connection == null) {
		// this.initialize();
		// }

		System.out.println("Initializing connection to database..");
		// if (connection != null) {
		// return connection; // singleton
		// }

		// try {
		// Load the JDBC driver
		System.out.println("Loading Database Driver...");
		Class.forName(driverName).newInstance();
		// Create a connection to the database
		String url = "jdbc:mysql://" + serverName + ":" + serverPort + "/"
				+ mydatabase; // a
		// JDBC
		// url
		System.out.println("Connecting to Database...");
		this.connection = DriverManager.getConnection(url, username, password);
		System.out.println("Connected to database.");
		// } catch (ClassNotFoundException e) {
		// Could not find the database driver
		// System.out.println("Cannot find the database driver");
		// } catch (SQLException e) {
		// Could not connect to the database
		// System.out.println("Cannot connect to database");
		// } catch (IllegalAccessException e) {
		// System.out.println("ERROR" + e);
		// } catch (InstantiationException e) {
		// System.out.println("ERROR" + e);
		// }

	}

	/**
	 * Gets the instance of the SQLConnection;
	 * 
	 * @return The instance of the SQLConnection.
	 */
	public static SQLConnection getInstance() {
		if (SQLConnection.connectionInstance == null) { // double checking
			synchronized (SQLConnection.class) {
				if (SQLConnection.connectionInstance == null) {
					SQLConnection sqlConnection;
					try {
						sqlConnection = new SQLConnection();
						SQLConnection.connectionInstance = sqlConnection;
						return SQLConnection.connectionInstance;
					} catch (InstantiationException e) {
						printError("ERROR:\n", e);
					} catch (IllegalAccessException e) {
						printError("ERROR:\n", e);
					} catch (ClassNotFoundException e) {
						printError("Cannot find the database driver", e);
					} catch (SQLException e) {
						printError("Cannot connect to database", e);
					}

				}
			}
		}

		if (SQLConnection.connectionInstance == null) {
			System.out.println("Connection CANNOT be created");
		}
		return SQLConnection.connectionInstance;
	}

	private static void printError(String message, Exception e) {
		// TODO Auto-generated catch block
		SQLConnection.connectionInstance = null;
		System.out.println(message);
		e.printStackTrace();
	}

	/**
	 * Returns the connection object
	 * 
	 * @return
	 */
	public Connection getConnection() {
		return connection;
	}

	/**
	 * Initializes the Connection
	 * 
	 * @return
	 */
	// private Connection initialize() {
	// System.out.println("Initializing connection to database..");
	// if (connection != null) {
	// return connection; // singleton
	// }
	//
	// try {
	// // Load the JDBC driver
	// System.out.println("Loading Database Driver...");
	// Class.forName(driverName).newInstance();
	// // Create a connection to the database
	// String url = "jdbc:mysql://" + serverName + ":" + serverPort + "/"
	// + mydatabase; // a
	// // JDBC
	// // url
	// System.out.println("Connecting to Database...");
	// connection = DriverManager.getConnection(url, username, password);
	// System.out.println("Connected to database.");
	// } catch (ClassNotFoundException e) {
	// // Could not find the database driver
	// System.out.println("Cannot find the database driver");
	// } catch (SQLException e) {
	// // Could not connect to the database
	// System.out.println("Cannot connect to database");
	// } catch (IllegalAccessException e) {
	// System.out.println("ERROR" + e);
	// } catch (InstantiationException e) {
	// System.out.println("ERROR" + e);
	// }
	//
	// return connection;
	// }

}